from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.mixins import CacheResponseMixin


from .serializers import AreaSerializer, SubsAreaSerializer
from .models import Area

# Create your views here.

class AreasViewSet(CacheResponseMixin, ReadOnlyModelViewSet):

    # """省市区数据查询"""

    pagination_class = None # 禁用分页，因为后面商品列表地方开启全部分页

    #指定序列化器
    def get_serializer_class(self):
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubsAreaSerializer

    #指定查询集
    def get_queryset(self):
        if self.action == 'list':
            return Area.objects.filter(parent=None) # 只返回省的数据
        else:
            return Area.objects.all()

